<template>
  <el-dialog
    title="添加用户"
    :visible="show"
    width="50%"
    @close="$emit('change-show',false)"
  >
    <template>
      <el-form :rules="rules" label-width="80px" :model="UserForm">
        <el-form-item prop="username" label="用户名">
          <el-input v-model.trim="UserForm.username" />
        </el-form-item>
        <el-form-item prop="password" label="密码">
          <el-input v-model.trim="UserForm.password" />
        </el-form-item>
        <el-form-item prop="email" label="邮箱">
          <el-input v-model.trim="UserForm.email" />
        </el-form-item>
        <el-form-item prop="mobile" label="手机号">
          <el-input v-model.trim="UserForm.mobile" />
        </el-form-item>
      </el-form>
    </template>
    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('change-show',false)">取 消</el-button>
      <el-button type="primary" @click="OnSave">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { AddUserInfo } from '@/api/user.js'
export default {
  props: {
    show: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      UserForm: {
        username: '坤坤',
        password: '123456',
        email: '123@123.com',
        mobile: '18788880000'
      }, // 添加用户所需数据
      isShow: false,

      // 自定义效验规则
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 2, max: 7, message: '长度在2到7个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 2, max: 7, message: '长度在2到7个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮件名不能为空', trigger: 'blur' },
          { pattern: /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/, message: '邮件格式不正确', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/, message: '手机号格式不正确', trigger: 'blur' }
        ]
      }
    }
  },

  created() {

  },

  methods: {
    async OnSave() {
      await AddUserInfo(this.UserForm)
      this.$emit('OnSuccess')
      this.$emit('change-show', false)
    }
  }
}
</script>

<style scoped lang='less'>

</style>
